package ar.com.muebleria.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JFrame;
import org.apache.log4j.Logger;
import ar.com.muebleria.beans.MenuItem;
import ar.com.muebleria.colecciones.MenuItems;
import ar.com.muebleria.util.BeanFactory;
import ar.com.muebleria.util.Parametros;
import javax.swing.JMenuBar;

public final class VistaPrincipal extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private MenuItems menuItems;
	/**
	 * This is the default constructor
	 */
	public VistaPrincipal() {
		super();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public void initialize() {
		Logger.getLogger("VistaPrincipal").info("Se inicia la ventana principal.");
		
//		this.setSize(Parametros.getInteger("ar.com.view.resolucionWidth")
//				    , Parametros.getInteger("ar.com.view.resolucionHeight"));
		this.setSize(800,600);
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle(Parametros.getString("ar.com.view.nombrePrograma"));
		this.setVisible(true);

	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
		}
		JMenu nuevo = null;
		boolean bandera = false;
		int contador = 0;
		for(MenuItem item : menuItems){
			contador++;
			if(item.getPadreId()==null){
				if(bandera)
					jJMenuBar.add(nuevo);
				nuevo = new JMenu(item.getDescripcion());
				bandera = true;
				continue;
			}
			JMenuItem menuItem = new JMenuItem();
			menuItem.setText(item.getDescripcion());
			menuItem.addActionListener(new Evento(item.getPrograma(), getJContentPane()));
			nuevo.add(menuItem);
			if(contador == menuItems.size())
				jJMenuBar.add(nuevo);
			
		}
		return jJMenuBar;
	}

	public void setMenuItems(MenuItems menuItems) {
		this.menuItems = menuItems;
	}

	public MenuItems getMenuItems() {
		return menuItems;
	}
	

}
